package exam.model.dao;

import java.util.HashMap;
import java.util.List;

import exam.common.key.KeyUser;
import exam.model.entity.TUserEntity;
import exam.system.dao.AbstractDAO;

public class TUserDAO extends AbstractDAO<TUserEntity>{
	
	public TUserDAO() {
		super(TUserEntity.class);
	}
	
	public List<TUserEntity> selectListUser() {
		return super.selectList("from TUserEntity");
	}
	
	public TUserEntity selectByPk(long id) {
		return super.selectByPk(id);
	}
	
	public TUserEntity selectByUserName(String username) {
		String query = "select u from TUserEntity u where u.userName=:userName";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("userName", username);
		return super.jdbcManager.executeByQueryParams(query, params).getSingleResult();
	}
	
	public int insertUser(TUserEntity user) {
		return super.insert(user);
	}
	
	/**
	 * updateUser
	 * @param tUserEntity
	 * @return
	 */
	public int updateUser(TUserEntity tUserEntity) {
		String query = "update TUserEntity u set u.userName=:userName, u.fullName=:fullName, " +
				"u.birthday=:birthday, u.gender=:gender, u.address=:address, u.role=:role," +
				"u.updateDate=:updateDate where u.userId=:userId";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("userName", tUserEntity.getUserName());
		params.put("fullName", tUserEntity.getFullName());
		params.put("birthday", tUserEntity.getBirthday());
		params.put("gender", tUserEntity.getGender());
		params.put("address", tUserEntity.getAddress());
		params.put("role", tUserEntity.getRole());
		params.put("updateDate", tUserEntity.getUpdateDate());
		params.put("userId", tUserEntity.getUserId());
		return jdbcManager.executeByQueryParams(query, params).getInt();
	}
	
	/**
	 * deleteUserByPk
	 * @param userId
	 * @return
	 */
	public void deleteUserByPk(long userId) {
//		String query = "delete from TUserEntity u where u.userId=:userId";
//		HashMap<String, Object> params = new HashMap<String, Object>();
//		params.put("userId", userId);
		super.delete(userId);
	}
	
	/**
	 * login
	 * @param userName
	 * @param password
	 * @return
	 */
	public String login(String userName, String password) {
		String query = "select u.userName from TUserEntity u where u.userName=:userName" +
				" and u.password=:password";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("userName", userName);
		params.put("password", password);
		return (String) jdbcManager.executeByQueryParams(query, params).getObjectResult();
	}
	
	/**
	 * changePassword
	 * @param passwordOld
	 * @param passwordNew
	 * @return
	 */
	public int changePassword(String passwordOld, String passwordNew) {
		String query = "update TUserEntity u set u.password=:password1 where u.password=:password2";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put(KeyUser.PASS_WORD_1, passwordOld);
		params.put(KeyUser.PASS_WORD_2, passwordNew);
		return jdbcManager.executeByQueryParams(query, params).getInt();
	}
}
